package com.carlife.ui;

import java.math.BigDecimal;
import java.util.Date;

import com.carlife.R;
import com.carlife.model.MileageLog;
import com.carlife.provider.CarStatusMgr;
import com.carlife.util.DateUtil;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.widget.LinearLayout;

public class Welcome extends Activity {
	
	private final String mimeType = "text/html";
	private final String encoding = "utf-8";
	
	public static void show(Context context) {
        final Intent intent = new Intent(context, Welcome.class);
        context.startActivity(intent);
    }
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.welcome);
        
        setupViews();
        
    }
    
    protected void setupViews() {
    	CarStatusMgr mgr = new CarStatusMgr(this);
    	MileageLog crntLog = mgr.getCrntMileage();
    	BigDecimal tmpMileage = mgr.getMileage(DateUtil.parseToFormat("2008-12-31", "yyyy-MM-dd", DateUtil.DF_ALL));
    	tmpMileage = tmpMileage.setScale(0, BigDecimal.ROUND_HALF_DOWN);    	
    	
    	WebView status = (WebView)findViewById(R.id.wv_status);
    	StringBuffer buffer = new StringBuffer();
    	
    	buffer.append("到 <font color='green' size='3'>"+ crntLog.get(MileageLog.FD_INPUT_TIME)+ "</font> 你的里程是 <font color='red' size='5'> "+ crntLog.get(MileageLog.FD_MILEAGE_VAlUE)+" </font> km. <br>");
    	buffer.append("到 <font color='green' size='3'> 2008-12-31 </font> 你的里程是 <font color='red' size='5'> "+ tmpMileage.toPlainString() +" </font> km. <br>");
    	buffer.append("你最近一月的油耗 <font color='red' size='5'> 10.1 </font> L/100KM.");
    	status.loadData(buffer.toString(), mimeType, encoding);
    	
    	LinearLayout oillife = (LinearLayout)findViewById(R.id.oillife);
    	registerForContextMenu(oillife);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.oillife, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        switch (item.getItemId()) {           
        case R.id.menu_item_home:
            // CarOil.show(this);
            return true;
        case R.id.menu_item_oil_input:
            OilInput.show(this);
            return true;
        case R.id.menu_item_mileage_input:
        	 MileageInput.show(this);
            return true;
        case R.id.menu_item_statics:
        	MileageLineChart.show(this);
            return true; 
        }

        return super.onMenuItemSelected(featureId, item);
    }

}